package hk.m4s.lr.repair.test.ui.user;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.MyOrderTuiModel;
import hk.m4s.lr.repair.test.service.user.MyService;
import hk.m4s.lr.repair.test.ui.adapter.MyTuiOderAdapter;
import hk.m4s.lr.repair.test.views.listview.VListView;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/3/23.
 * 退款退货
 */

public class MyTuioderActivity extends UeBaseActivity implements VListView.IXListViewListener {
    /**
     * x下拉列表
     */
    private VListView vlist;
    private int pageNum = 1;
    private int pageSize = 10;
    private MyTuiOderAdapter myOderAdapter;
    private String state = "";
    private List<MyOrderTuiModel.ListBean> myjiangLiModelList = new ArrayList<>();
   private LinearLayout show_img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_my_tui_order);
        hiddenFooter();
        showGoBackBtn();
        setTitleText("退款/退货");
        findBaseView();
        vlist.setPullLoadEnable(false);
    }

    public void findBaseView() {
        show_img=(LinearLayout)findViewById(R.id.show_img);
        vlist = (VListView) findViewById(R.id.vlist);
        vlist.setXListViewListener(this);
        vlist.setPullRefreshEnable(false);
        vlist.setPullLoadEnable(false);
        myOderAdapter = new MyTuiOderAdapter(MyTuioderActivity.this, myjiangLiModelList);
        vlist.setAdapter(myOderAdapter);
        onLoad();
       findUserReturnOrder("1");
    }

    private void onLoad() {
        vlist.stopRefresh();
        vlist.stopLoadMore();
        Date data = new Date(System.currentTimeMillis());
        vlist.setRefreshTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.CHINA).format(data));
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        vlist.setPullLoadEnable(false);
        pageNum = 1;
        //findUserReturnOrder("1");
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        vlist.setPullRefreshEnable(false);
        pageNum++;
        //findUserReturnOrder("2");
    }


    public void findUserReturnOrder(final String refreshType) {
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("device_type", Constant.device_type);
        //parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
        parmas.put("token","37859dc04fe9481d948e9cc5ee56c679");
       // parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
        parmas.put("jsonText", "RyQdVk2IBDp6SxdwAB8G27gQnXP1JQnvVmLqjrQbNeaB9EqbDGKMYbrghgxRjFbLeZQNj+9yRCCPb9Q/fCqoVpmihH2K2wQ0FGhq8AKmB7blIJfrI/e8NXdVYRVIiURh7k1NlD7iDQoWP4JGlBVVvD/2+DxFbgP5kFyJj8SjJuA=");
        MyService.findUserReturnOrder(MyTuioderActivity.this, parmas, new ResponseCallback<MyOrderTuiModel>() {
            @Override
            public void onSuccess(MyOrderTuiModel model) {
                onLoad();
                if(model.getList()!=null){
                    switch (refreshType) {
                        case "1":
                            myjiangLiModelList.clear();
                            if(model.getList()!=null) {
                                show_img.setVisibility(View.GONE);
                                myjiangLiModelList.addAll(model.getList());
                                if (model.getList().size() < pageSize) {
                                    vlist.removeFooterView();
                                    vlist.setPullLoadEnable(false);
                                } else {

                                    vlist.setPullLoadEnable(true);
                                }
                            }else{
                                show_img.setVisibility(View.VISIBLE);
                            }
                            break;
                        case "2":
                            break;
                    }
                    myOderAdapter.notifyDataSetChanged();

                }

            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }
    public void update(){
        findUserReturnOrder("1");
    }
}
